//获取元素
var but = document.getElementById("but");
var input = document.getElementsByTagName("input");

//定义数组
let yong = [];
let zhang = [];
let mi = [];
let dian = [];

//添加本地储存的数据
try {
  for (var i = 0; i < JSON.parse(localStorage.getItem("yong")).length; i++) {
    yong.push(JSON.parse(localStorage.getItem("yong"))[i]);
    zhang.push(JSON.parse(localStorage.getItem("zhang"))[i]);
    mi.push(JSON.parse(localStorage.getItem("mi"))[i]);
    dian.push(JSON.parse(localStorage.getItem("dian"))[i]);
  }
} catch (error) {}

//点击提交
but.onclick = function () {
  //判断是否输入
  for (var i = 0; i < input.length; i++) {
    if (input[i].value == "") {
      alert("请输入信息");
      return 0;
    }
  }

  //判断账号是否重复
  if (input[1].value == 1) {
    alert("该账号已注册");
    return 0;
  }

  try {
    for (var i = 0; i < JSON.parse(localStorage.getItem("zhang")).length; i++) {
      if (input[1].value == JSON.parse(localStorage.getItem("zhang"))[i]) {
        alert("该账号已注册");
        return 0;
      }
    }
  } catch (error) {}

  // 将数据存入本地存储
  yong.push(input[0].value);
  localStorage.setItem("yong", JSON.stringify(yong));

  zhang.push(input[1].value);
  localStorage.setItem("zhang", JSON.stringify(zhang));

  mi.push(input[2].value);
  localStorage.setItem("mi", JSON.stringify(mi));

  dian.push(input[3].value);
  localStorage.setItem("dian", JSON.stringify(dian));

  //注册成功跳转页面
  alert("注册成功");
  location.href = "登录页面.html";
};
